﻿---@diagnostic disable: trailing-space
local extension = Package:new("rfenghou_cards", Package.CardPack)
extension.extensionName = "aaa_fenghou"

Fk:loadTranslationTable{
  ["rfenghou_cards"] = "封侯卡牌",
}

local U = require "packages/utility/utility"

local slash = Fk:cloneCard("slash")

local shootSlashSKill = fk.CreateActiveSkill{
  name = "rfenghou_shoot__slash_skill",
  prompt = slash.skill.prompt,
  max_phase_use_time = 1,
  target_num = 1,
  can_use = slash.skill.canUse,
  mod_target_filter = function (self, to_select, selected, player, card, distance_limited, extra_data)
    return to_select ~= player.id
  end,
  target_filter = slash.skill.targetFilter,
  on_effect = function (self, room, effect)
    local from = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.to)
    if not to.dead then
      room:damage({
        from = from,
        to = to,
        card = effect.card,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = self.name
      })
    end
  end
}
Fk:addSkill(shootSlashSKill)

local shoot__slash = fk.CreateBasicCard{
  name = "&rfenghou_shoot__slash",
  skill = shootSlashSKill,
}
extension:addCard(shoot__slash)

Fk:loadTranslationTable{
  ["rfenghou_shoot__slash"] = "射杀",
  [":rfenghou_shoot__slash"] = "基本牌"
  .."<br/><b>时机</b>：出牌阶段限一次"
  .."<br/><b>目标</b>：一名其他角色"
  .."<br/><b>效果</b>：对目标角色造成1点伤害",

  ["rfenghou_shoot__slash_skill"] = "射杀",
}

-- 搬运界标EX木马

local rfenghou__wooden_ox_skill = fk.CreateActiveSkill{
  name = "rfenghou__wooden_ox_skill",
  attached_equip = "rfenghou__wooden_ox",
  prompt = "#rfenghou__wooden_ox-prompt",
  can_use = function(self, player, card)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getPile("$role_carriage") < 5
  end,
  card_num = 1,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and table.contains(Self.player_cards[Player.Hand], to_select)
  end,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:addToPile("$role_carriage", effect.cards[1], false, self.name)
    if player.dead then return end
    local ox = table.find(player:getCardIds("e"), function (id) return Fk:getCardById(id).name == "rfenghou__wooden_ox" end)
    if ox then
      local targets = table.filter(room.alive_players, function(p)
        return p ~= player and p:hasEmptyEquipSlot(Card.SubtypeTreasure) end)
      if #targets > 0 then
        local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#rfenghou__wooden_ox-move", self.name, true)
        if #tos > 0 then
          room:moveCardTo(ox, Card.PlayerEquip, tos[1], fk.ReasonPut, self.name, nil, true, player.id, nil)
        end
      end
    end
  end,
}
local rfenghou__wooden_ox_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__wooden_ox_trigger",
  attached_equip = "rfenghou__wooden_ox",
  mute = true,
  priority = 5,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:getPile("$role_carriage") == 0 then return false end
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if Fk:getCardById(info.cardId).name == "rfenghou__wooden_ox" then
          --多个木马同时移动的情况取其中之一即可，不再做冗余判断
          if info.fromArea == Card.Processing then
            local room = player.room
            --注意到一次交换事件的过程中的两次移动事件都是在一个parent事件里进行的，因此查询到parent事件为止即可
            local move_event = room.logic:getCurrentEvent():findParent(GameEvent.MoveCards, true)
            local parent_event = move_event.parent
            local move_events = room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
              if e.id >= move_event.id or e.parent ~= parent_event then return false end
              for _, last_move in ipairs(e.data) do
                if last_move.moveReason == fk.ReasonExchange and last_move.toArea == Card.Processing then
                  return true
                end
              end
            end, parent_event.id)
            if #move_events > 0 then
              for _, last_move in ipairs(move_events[1].data) do
                if last_move.moveReason == fk.ReasonExchange then
                  for _, last_info in ipairs(last_move.moveInfo) do
                    if Fk:getCardById(last_info.cardId).name == "rfenghou__wooden_ox" then
                      if last_move.from == player.id and last_info.fromArea == Card.PlayerEquip then
                        if move.toArea == Card.PlayerEquip then
                          if move.to ~= player.id then
                            self.cost_data = move.to
                            return true
                          end
                        else
                          self.cost_data = nil
                          return true
                        end
                      end
                    end
                  end
                end
              end
            end
          elseif move.moveReason == fk.ReasonExchange then
            if move.from == player.id and info.fromArea == Card.PlayerEquip and move.toArea ~= Card.Processing then
              --适用于被修改了移动区域的情况，如销毁，虽然说原则上移至处理区是不应销毁的
              self.cost_data = nil
              return true
            end
          elseif move.from == player.id and info.fromArea == Card.PlayerEquip then
            if move.toArea == Card.PlayerEquip then
              if move.to ~= player.id then
                self.cost_data = move.to
                return true
              end
            else
              self.cost_data = nil
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, _, player, data)
    local room = player.room
    local cards = player:getPile("$role_carriage")
    local to = self.cost_data
    if to then
      room:getPlayerById(to):addToPile("$role_carriage", cards, false, "rfenghou__wooden_ox_skill", nil, {player.id, to})
    else
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "rfenghou__wooden_ox_skill", nil, true)
    end
  end,
}
local rfenghou__wooden_ox_filter = fk.CreateFilterSkill{
  name = "#rfenghou__wooden_ox_filter",

  handly_cards = function (self, player)
    if player:hasSkill(rfenghou__wooden_ox_skill) then
      return player:getPile("$role_carriage")
    end
  end,
}
rfenghou__wooden_ox_skill:addRelatedSkill(rfenghou__wooden_ox_trigger)
rfenghou__wooden_ox_skill:addRelatedSkill(rfenghou__wooden_ox_filter)
Fk:addSkill(rfenghou__wooden_ox_skill)
local rfenghou__wooden_ox = fk.CreateTreasure{
  name = "rfenghou__wooden_ox",
  suit = Card.Diamond,
  number = 5,
  equip_skill = rfenghou__wooden_ox_skill,

  on_uninstall = function(self, room, player)
    Treasure.onUninstall(self, room, player)
    player:setSkillUseHistory(self.equip_skill.name, 0, Player.HistoryPhase)
  end,
}
extension:addCard(rfenghou__wooden_ox)
Fk:loadTranslationTable{
  ["rfenghou__wooden_ox"] = "木牛流马",
  [":rfenghou__wooden_ox"] = "装备牌·宝物<br/><b>宝物技能</b>：<br/>" ..
  "出牌阶段限一次，你可将一张手牌扣置于装备区里的【木牛流马】下（称为“辎”，“辎”至多有5张），然后你可以将【木牛流马】置入一名"..
  "其他角色的装备区。你可以如手牌般使用或打出“辎”。",
  ["wooden_ox"] = "木牛流马",
  ["rfenghou__wooden_ox_skill"] = "木牛",
  [":rfenghou__wooden_ox_skill"] = "出牌阶段限一次，你可将一张手牌扣置于装备区里的【木牛流马】下（称为“辎”，“辎”至多有5张），"..
  "然后你可以将【木牛流马】置入一名其他角色的装备区。你可以如手牌般使用或打出“辎”。",
  ["#rfenghou__wooden_ox-move"] = "你可以将【木牛流马】移动至一名其他角色的装备区",
  ["$role_carriage"] = "辎",
  ["#rfenghou__wooden_ox_filter"] = "辎",
  ["new_role_cards"] = "新身份模式卡牌",
  ["#rfenghou__wooden_ox_trigger"] = "木牛流马",
  ["#rfenghou__wooden_ox-prompt"] = "你可以将一张手牌扣置于【木牛流马】下",
}


local drugs_skill = fk.CreateActiveSkill{
  name = "drugs_skill",
  prompt = "#drugs_skill",
  mod_target_filter = Util.TrueFunc,
  can_use = Util.SelfCanUse,
  on_use = function(self, room, cardUseEvent)
    if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
      cardUseEvent.tos = { { cardUseEvent.from } }
    end
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if to.dead then return end
    local n = 1 + ((effect.extra_data or Util.DummyTable).additionalBuff or 0)
    if to:isWounded() then
      room:recover { num = n, skillName = self.name, who = to, recoverBy = room:getPlayerById(effect.from), card = effect.card }
      if to.dead then return end
      room:addPlayerMark(to, "@drugsInflicted", n)
    end
    if not to:isWounded() then
      room:addPlayerMark(to, "drugs_buff-phase", n)
    end
  end
}

local drugs = fk.CreateBasicCard{
  name = "drugs",
  suit = Card.Club,
  number = 1,
  skill = drugs_skill,
} 

local drugs_trigger = fk.CreateTriggerSkill{
  name = "#drugs_trigger",
  global = true,
  mute = true,
  priority = 0.01,
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if target == player then
      if event == fk.DamageInflicted then
        return player:getMark("@drugsInflicted") > 0
      else
        return player:getMark("@drugsCaused") > 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      data.damage = data.damage + player:getMark("@drugsInflicted")
      room:setPlayerMark(player, "@drugsInflicted", 0)
    else
      data.damage = data.damage + player:getMark("@drugsCaused")
      room:setPlayerMark(player, "@drugsCaused", 0)
    end
  end,
}
Fk:addSkill(drugs_trigger)

local drugs_targetmod = fk.CreateTargetModSkill{
  name = "drugs_targetmod",
  global = true,
  residue_func = function(self, player, skill, scope)
    if skill then
      if skill.trueName == "slash_skill" or skill.trueName == "analeptic_skill" then
        return player:getMark("drugs_buff-phase")
      end 
    end
  end,
}
Fk:addSkill(drugs_targetmod)

extension:addCards({
  drugs,
  drugs:clone(Card.Club, 13),
})

Fk:loadTranslationTable{
  ["drugs"] = "散",
	[":drugs"] = "基本牌<br /><b>时机</b>：出牌阶段<br /><b>目标：</b>你<br /><b>效果：</b>若你已受伤，回复1点体力且下次受到的伤害+1。若你未受伤，或因此回复至满血，本阶段使用【杀】与【酒】次数+1。",
  ["drugs_skill"] = "散",
  ["#drugs_skill"] = "已受伤：回复体力且下次受到伤害+1；未受伤/因此回满血：使用【杀】与【酒】次数+1",
  ["@drugsInflicted"] = "散:受伤+",
  ["#drugs_trigger"] = "散",
}



local wild_revolt_skill = fk.CreateActiveSkill{
  name = "wild_revolt_skill",
  prompt = "#wild_revolt_skill",
  can_use = Util.GlobalCanUse,
  on_use = Util.GlobalOnUse,
  mod_target_filter = Util.TrueFunc,
  on_action = function(self, room, use, finished)
    use.extra_data = use.extra_data or {}
    if not finished then
      use.extra_data.WildRevoltInfo = {}
    else
      local info = use.extra_data.WildRevoltInfo
      if info then
        local targets = table.map(table.filter(use.extra_data.WildRevoltInfo, function(id) return id ~= 0 end), Util.Id2PlayerMapper)
        local bad = #targets > (#info / 2)
        use.extra_data.WildRevoltInfo = nil
        for _, p in ipairs(targets) do
          if not p.dead then
            if bad then
              room:damage { from = nil, to = p, damage = 1, skillName = self.name, card = use.card }
            else
              local tos = room:askForChoosePlayers(p, table.map(room.alive_players, Util.IdMapper), 1, 1, "#wild_revolt-choose",
              self.name, false)
              room:damage { from = p, to = room:getPlayerById(tos[1]), damage = 1, skillName = self.name, card = use.card }
            end
          end
        end
      end
    end
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if effect.extra_data and effect.extra_data.WildRevoltInfo then
      if room:askForSkillInvoke(to, self.name, nil, "#wild_revolt-ask") then
        table.insert(effect.extra_data.WildRevoltInfo, to.id)
      else
        table.insert(effect.extra_data.WildRevoltInfo, 0)
      end
    end
  end
}

local wild_revolt = fk.CreateTrickCard{
  name = "wild_revolt",
  suit = Card.Spade,
  number = 13,
  multiple_targets = true,
  is_damage_card = true,
  skill = wild_revolt_skill,
}

extension:addCard(wild_revolt)

Fk:loadTranslationTable{
  ["wild_revolt"] = "群逆纵逸",
	[":wild_revolt"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标：</b>所有角色<br /><b>效果：</b>目标角色秘密选择是否于结算后分配1点伤害，若选择是的数量多于一半，改为受到伤害。",
  ["wild_revolt_skill"] = "群逆纵逸",
  ["#wild_revolt_skill"] = "令所有角色选择是否分配伤害，若选择是的角色多于一半，改为受到伤害",
  ["#wild_revolt-ask"] = "群逆纵逸:是否选择在结算后分配1点伤害？",
  ["#wild_revolt-choose"] = "群逆纵逸:请分配1点伤害！",
}




return extension
